/*
 * This file is part of aion-lightning <aion-lightning.org>.
 * 
 * aion-lightning is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * aion-lightning is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with aion-lightning.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.services;

import com.aionemu.commons.network.util.ThreadPoolManager;
import com.aionengine.gameserver.dataholders.DataManager;
import com.aionengine.gameserver.model.curingzone.CuringObject;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.templates.curingzones.CuringTemplate;
import com.aionengine.gameserver.skillengine.SkillEngine;
import com.aionengine.gameserver.utils.MathUtil;
import com.aionengine.gameserver.world.knownlist.Visitor;
import javolution.util.FastList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xTz
 */
public class CuringZoneService {

    Logger log = LoggerFactory.getLogger(CuringZoneService.class);
    private FastList<CuringObject> curingObjects = new FastList<CuringObject>();

    private CuringZoneService() {
        for (CuringTemplate t : DataManager.CURING_OBJECTS_DATA.getCuringObject()) {
            CuringObject obj = new CuringObject(t, 0);
            obj.spawn();
            curingObjects.add(obj);
        }
        log.info("spawned Curing Zones");
        startTask();
    }

    private void startTask() {

        ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                for (final CuringObject obj : curingObjects) {
                    obj.getKnownList().doOnAllPlayers(new Visitor<Player>() {

                        @Override
                        public void visit(Player player) {
                            if (MathUtil.isIn3dRange(obj, player, obj.getRange()) && !player.getEffectController().hasAbnormalEffect(8751)) {
                                SkillEngine.getInstance().getSkill(player, 8751, 1, player).useNoAnimationSkill();
                            }
                        }

                    });
                }
            }

        }, 1000, 1000);

    }

    private static class SingletonHolder {

        protected static final CuringZoneService instance = new CuringZoneService();
    }

    public static final CuringZoneService getInstance() {
        return SingletonHolder.instance;
    }

}